package com.maila.simplerecord.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.maila.simplerecord.R;
import com.maila.simplerecord.adapter.HomeProjectAdapter;
import com.maila.simplerecord.data.Projectdata;
import com.maila.simplerecord.flingswipe.SwipeFlingAdapterView;
import com.maila.simplerecord.utils.StartActivityUtils;
import com.zhy.autolayout.AutoLayoutActivity;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AutoLayoutActivity implements SwipeFlingAdapterView.onFlingListener,
        SwipeFlingAdapterView.OnItemClickListener, View.OnClickListener{

    private int cardWidth;
    private int cardHeight;

    private SwipeFlingAdapterView swipeView;
    private HomeProjectAdapter adapter;
    private AutoRelativeLayout gotoall,addnew;
    private TextView ProjectNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        if(Projectdata.getAll().size()<1){
            Projectdata project1=new Projectdata();
            project1.setPid(1);
            project1.setAddress("山东省潍坊市高新区东方路樱前街大观天下");
            project1.setProject_name("大观天下20楼2301");
            project1.setClient_name("李女士");
            project1.setStart_time("2017.06.11");
            project1.setEnd_time("09.29");
            project1.setClient_phone("15064620701");
            project1.hide=1;
            project1.save();
            Projectdata project2=new Projectdata();
            project2.setPid(2);
            project2.hide=1;
            project2.save();
        }
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(Projectdata.getcount()>0){
            ProjectNum.setVisibility(View.VISIBLE);
            ProjectNum.setText(Projectdata.getcount()+"");
        }else {
            ProjectNum.setVisibility(View.GONE);
        }

        initdata();
    }

    private void initdata() {
        List<Projectdata> mdata=Projectdata.getAll(1);
        for(int i=0;i<mdata.size();i++){
            removeFirstObjectInAdapter();
        }
        adapter.addAll(mdata);
        swipeView.forceRequestLayout();

    }

    private void initView() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        float density = dm.density;
        cardWidth = (int) (dm.widthPixels - (2 * 18 * density));
        cardHeight = (int) (dm.heightPixels - (338 * density));
        swipeView = (SwipeFlingAdapterView) findViewById(R.id.swipe_view);
        gotoall= (AutoRelativeLayout) findViewById(R.id.dummy_button1);
        addnew= (AutoRelativeLayout) findViewById(R.id.dummy_button);
        ProjectNum=(TextView) findViewById(R.id.project_num);
        gotoall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent();
                it.setClass(FullscreenActivity.this, AllProjectListActivity.class);
                startActivity(it);
            }
        });
        addnew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartActivityUtils.startByOpt(FullscreenActivity.this,SubjectDetailActivity.class,0,1);
            }
        });
        if (swipeView != null) {
            swipeView.setIsNeedSwipe(true);
            swipeView.setFlingListener(this);
            swipeView.setOnItemClickListener(this);
            adapter = new HomeProjectAdapter(this);
            swipeView.setAdapter(adapter);
        }
    }




    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClicked(MotionEvent event, View v, Object dataObject) {
        if(dataObject instanceof Projectdata){
            StartActivityUtils.startByOpt(FullscreenActivity.this,SubjectDetailActivity.class,((Projectdata)dataObject).pid,0);
        }

    }

    @Override
    public void removeFirstObjectInAdapter() {
        adapter.remove(0);
    }

    @Override
    public void onLeftCardExit(Object dataObject) {

    }

    @Override
    public void onRightCardExit(Object dataObject) {

    }

    @Override
    public void onAdapterAboutToEmpty(int itemsInAdapter) {
        if (itemsInAdapter == 1) {
            List<Projectdata> mdata=Projectdata.getAll(1);
            adapter.addAll(mdata);
        }
    }

    @Override
    public void onScroll(float progress, float scrollXProgress) {

    }
}
